package com.ftg.learn.bus.sharecar.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.bus.sharecar.mapper.sharecar_mapper;
import com.ftg.learn.bus.sharecar.pojo.sharecar;
import com.ftg.learn.bus.util.ResultType;
import com.ftg.learn.bus.util.ToExcel;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.filechooser.FileSystemView;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class sharecar_service {
    @Autowired
    sharecar_mapper sm;

    /**
     * 查询
     * @return
     */
    public List<sharecar> query(){

        return sm.query();
    }

    /**
     * 添加
     * @param s
     * @return
     */
    public int add(sharecar s) {
        if(s.getCar_name().equals("")||s.getCar_name()==null||
                s.getCar_id().equals("")||s.getCar_id()==null||
                s.getEngine_number().equals("")||s.getEngine_number()==null||
                s.getVin().equals("")||s.getVin()==null||
                s.getApparatus_number().equals("")||s.getApparatus_number()==null||
                s.getRecipient().equals("")||s.getRecipient()==null||
                s.getPrincipal().equals("")||s.getPrincipal()==null||
                s.getAffiliated_unit().equals("")||s.getAffiliated_unit()==null||
                s.getManufacturer().equals("")||s.getManufacturer()==null||
                s.getPark_loc().equals("")||s.getPark_loc()==null||
                s.getDisplacement().equals("")||s.getDisplacement()==null||
                s.getType().equals("")||s.getType()==null||
                s.getBuydate().equals("")||s.getBuydate()==null||
                s.getSort().equals("")||s.getSort()==null||
                s.getUsefor().equals("")||s.getUsefor()==null||
                s.getSharecar_photo().equals("")||s.getSharecar_photo()==null||
                s.getCreatedate().equals("")||s.getCreatedate()==null||
                s.getUpdatedate().equals("")||s.getUpdatedate()==null
        ){
            return -1;
        }
        if(queryone(s.getCar_name())==null){
            return -1;
        }
            return sm.insert(s);
    }

    /**
     * 查一条
     * @param
     * @return
     */
    public sharecar queryone(String car_name) {
        if(car_name==null||car_name.equals("")){
            throw new RuntimeException("id不能为空");
        }
        return sm.selectById(car_name);
    }


    public int update(sharecar s,String car_name) {
        if(s.getCar_name().equals("")||s.getCar_name()==null||
                s.getCar_id().equals("")||s.getCar_id()==null||
                s.getEngine_number().equals("")||s.getEngine_number()==null||
                s.getVin().equals("")||s.getVin()==null||
                s.getApparatus_number().equals("")||s.getApparatus_number()==null||
                s.getRecipient().equals("")||s.getRecipient()==null||
                s.getPrincipal().equals("")||s.getPrincipal()==null||
                s.getAffiliated_unit().equals("")||s.getAffiliated_unit()==null||
                s.getManufacturer().equals("")||s.getManufacturer()==null||
                s.getPark_loc().equals("")||s.getPark_loc()==null||
                s.getDisplacement().equals("")||s.getDisplacement()==null||
                s.getType().equals("")||s.getType()==null||
                s.getBuydate().equals("")||s.getBuydate()==null||
                s.getSort().equals("")||s.getSort()==null||
                s.getUsefor().equals("")||s.getUsefor()==null||
                s.getSharecar_photo().equals("")||s.getSharecar_photo()==null||
                s.getCreatedate().equals("")||s.getCreatedate()==null||
                s.getUpdatedate().equals("")||s.getUpdatedate()==null
        ){
            return -1;
        }
        if(queryone(car_name)==null){
            return -1;
        }
        QueryWrapper q=new QueryWrapper();
        q.eq("car_name",car_name);
        return sm.update(s,q);
    }

    /**
     * 删除
     * @param car_name
     * @return
     */
    public int delete(String car_name) {
        return sm.deleteById(car_name);
    }

    /**
     * 分页查询
     * @param page        --页数
     * @param limit       --每页显示条目数
     * @param car_name
     * @param car_id
     * @param engine_number
     * @param vin
     * @param apparatus_number
     * @param recipient
     * @param principal
     * @param affiliated_unit
     * @param manufacturer
     * @param park_loc
     * @param displacement
     * @param type
     * @param buydate
     * @param sort
     * @param
     * @param usefor
     * @param sharecar_photo
     * @param    --实体类主键  用于模糊查询
     * @param principal   --负责人     用于模糊查询
     * @return
     */
    public ResultType<sharecar> query(int page, int limit, String car_name, String car_id, String engine_number, String vin, String apparatus_number, String recipient, String principal, String affiliated_unit, String manufacturer, String park_loc, String displacement, String type, String buydate, String sort, String usefor, String sharecar_photo, String createdate,String updatedate) {
        ResultType<sharecar> rs = new ResultType<>();
        rs.setCode(0);
        IPage<sharecar> ip = new Page<>(page,limit);
        QueryWrapper<sharecar> qw=new QueryWrapper<>();
        qw.and(wrapper->wrapper.like("car_name",car_name)
                .like("car_id",car_id)
                .like("engine_number",engine_number)
                .like("vin",vin)
                .like("apparatus_number",apparatus_number)
                .like("recipient",recipient)
                .like("principal",principal)
                .like("affiliated_unit",affiliated_unit)
                .like("manufacturer",manufacturer)
                .like("park_loc",park_loc)
                .like("displacement",displacement)
                .like("type",type)
                .like("buydate",buydate)
                .like("sort",sort)
                .like("usefor",usefor)
                .like("sharecar_photo",sharecar_photo)
                .like("createdate",createdate)
                .like("updatedate",updatedate));
        ip =  sm.selectPage(ip,qw);
        rs.setData(ip.getRecords());
        rs.setCount(ip.getTotal());
        return rs;
    }


    /**
     * 导出为excel
     * @param url
     * @param
     */
    public String exportExcel(String url){
        if(url!=null || !url.equals("")){
            ToExcel t=new ToExcel();
            try {
                t.WriteExcel(url="","公车信息",sm.selectMaps(null),sm.selectColumnNameAndComment());
                return "成功";
            }catch (Exception e){
                return "失败";
            }
        }
        return "失败";
    }

    /**
     * 导入数据
     * @param url
     * @return
     */
    public String toLeadExcel(String url){
        if(url!=null || !url.equals("")){
            ToExcel t=new ToExcel();
            try {
                FileSystemView fsv = FileSystemView.getFileSystemView();
                File com=fsv.getHomeDirectory();
                List<Map<String,Object>> list=t.toLeadExcel(com.getPath()+"\\公车信息.xlsx",sm.selectColumnNameAndComment());
                Class cl= sharecar.class;//改成对应pojo类
                List<sharecar> lob=new ArrayList<>();
                for(int j=0;j<list.size();j++){
                    sharecar t1 = (sharecar)cl.newInstance();//改成对应pojo类
                    Field[] fields=cl.getDeclaredFields();
                    for(Field field:fields){
                        String fieldName=field.getName();
                        PropertyDescriptor pd = new PropertyDescriptor(fieldName, cl);
                        Method wM = pd.getWriteMethod();
                        for(int i=0;i<list.get(j).size();i++){
                            wM.invoke(t1, list.get(j).get(fieldName));
                        }
                    }
                    lob.add(t1);
                }
                for(sharecar s:lob){
                    sm.insert(s);
                }
                return "成功";
            }catch (Exception e){
                return "失败";
            }
        }
        return "失败";
    }
}
